import React, { Component } from "react";
import './index.css'

export default class Footer extends Component {

    selectAll = ( event ) =>{
        // event.target.checked
        this.props.selectAllStateChange( event.target.checked )
    }

    clearHasDone = () => {
        if( window.confirm("确定要清除已完成任务嘛？") ){
            this.props.clearHasDone();
        }
    }


    render() {
        const { todos } = this.props
        // 总数
        let total = todos.length; 
        // 已完成个数
        let hasDone = todos.reduce( ( prev, todo ) => {
            return prev + ( todo.done ? 1 : 0)
        }, 0)

        return (
            <div>
                <div className="todo-footer">
                    <label>
                        <input type="checkbox" checked={ hasDone === total && total !== 0 } onChange={ this.selectAll }/>
                    </label>
                    <span>
                        <span>已完成{hasDone}</span> / 全部{ total }
                    </span>
                    <button className="btn btn-danger" style={{ display: hasDone === 0 ? 'none' : "block" }} onClick={ this.clearHasDone }>清除已完成任务</button>
                </div>
            </div>
        );
    }
}
